﻿package
{

	import ASF;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;

	import Delay;
	
	import StateGameLoop;
	
	public class ASSeeker extends FlxSprite
	{
		private static const MODE_NORMAL:uint	= 0; // Will merely try to collide with the player
		private static const MODE_VICIOUS:uint	= 1; // Will hover along the ground, determined to kill!  Hyperspaaaace!
		
		[Embed(source = 'data/seeker.png')] public var imgSeeker:Class;
		[Embed(source = 'data/seeker_loop.mp3')] public var sndSeeker:Class;
		
		public var _parentState:StateGameLoop;
		public var _snd:Sound;
		private var _sndCh:SoundChannel;
		private var _assaultSpeed:uint;
		private var _mode:uint;
		private var _assaultDir:int;
	
		/*----------------------------------------------*/
		public function ASSeeker(ParentState:StateGameLoop)
		{
			//super(imgSeeker, 0, 0, true, false, width, 7, 7, false);
			super();
			width = 7;
			height = 7;
			loadGraphic(imgSeeker, true, false, width, height, false);
			
			addAnimation("seeker", [0, 1, 2], 5);
			_parentState = ParentState;
			color = 0xFFFFFFFF;
			_snd = new sndSeeker() as Sound;
			play("seeker");
			init();
		}

		/*----------------------------------------------*/
		public function init():void
		{
			exists = false;
			visible = false;
			x = y = 0;
			_mode = MODE_NORMAL;
			_assaultDir = 0;
		}
		
		/*----------------------------------------------*/
		public function spawn():void
		{
			x = FlxG.random() * (FlxG.width - width);
			y = 0;
			
			_assaultSpeed = Utils.randRange(10, ASF.SEEKER_SPEED_X);
			
			velocity.y = ASF.SEEKER_SPEED_Y;
									
			exists = true;
			visible = true;

			if (FlxG.random()*1 == 1)
			
			play("seeker");
			_sndCh = _snd.play(0,9999);
		}
		
		/*----------------------------------------------*/
		override public function update():void
		{
			x = int(x);
			switch(_mode) {
				case MODE_VICIOUS:
					if (y >= ASF.GROUND_LEVEL - height) {
						// TODO
						//_assaultDir = ;
					}
					break;
				case MODE_NORMAL:
				default:
					if (y > ASF.GROUND_LEVEL) {
						hurt(1);
						exists = false;
						//FlxG.play(sndHitGround, 0.25);
						_parentState.adjustScore(ASF.SCORE_PENALTY_ROCK_SMALL_LANDS);
					}
				
					break;
			}
			if (x < (_parentState._player.x+4)) {
				velocity.x = _assaultSpeed;
			} else if (x > (_parentState._player.x+4)) {
				velocity.x = -_assaultSpeed;
			} else {
				velocity.x = 0;
			}
						
			// Sometimes they trail off the screen due to horizontal velocity -- fuck it, you lucked out
			if (!onScreen()) {
				exists = false;
			}
			
			
			super.update();
		}
		
		/*----------------------------------------------*/
		override public function hurt(Damage:Number):void
		{
			exists = false;
			visible = false;
			_sndCh.stop();
			
		}
		
		/*----------------------------------------------*/
		public function getScore():int
		{
			return ASF.SCORE_SEEKER;
		}
	}
	
}